package com.zdlog.wenjuan.service.impl;

import com.zdlog.wenjuan.VO.ResultBoolenVO;
import com.zdlog.wenjuan.dao.MainProblemRepository;
import com.zdlog.wenjuan.dao.ProblemDetailRepository;
import com.zdlog.wenjuan.dataobject.MainProblem;
import com.zdlog.wenjuan.dataobject.ProblemDetail;
import com.zdlog.wenjuan.service.MainProblemService;
import com.zdlog.wenjuan.service.ProblemService;
import com.zdlog.wenjuan.util.ResultBoolenVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: wenjuanSer
 * @PACKAGE_NAME: com.zdlog.wenjuan.service.impl
 * @author: Kuaile
 * @create: 2018-02-06 17:14
 **/
@Service
public class MainProblemServiceImpl implements MainProblemService{


    @Autowired
    private MainProblemRepository repository;


    @Autowired
    private ProblemService problemService;


    @Autowired
    private ProblemDetailRepository problemDetailRepository;


    @Override
    public MainProblem save(MainProblem mainProblem) {
        return repository.save(mainProblem);
    }

    @Override
    public List<MainProblem> list(String formId) {
        return repository.findByFormIdOrderBySerialNumberAsc(formId);
    }

    @Override
    public ResultBoolenVO deleteMainProblemAndOption(String mainProblemId) {
        repository.delete(mainProblemId);
        List<ProblemDetail> problemDetailList = problemDetailRepository.findByMainProblemIdOrderBySerialNumberAsc(mainProblemId);
        if (problemDetailList != null) {
            for (ProblemDetail problemDetail : problemDetailList) {
                problemService.delete(problemDetail.getProblemDetailId());
            }
        }
        return ResultBoolenVOUtil.success();
    }
}
